package com.zf.shuashua.advert.mg;

import android.content.Context;

import androidx.annotation.NonNull;

import com.zf.shuashua.R;
import com.zf.shuashua.net.Api;

import java.util.List;

public class MangoReward {

    private Context mContext;
    private static MGRewardListener mangoRewardListener;
    private AdDataBean mAdDataBean;

    public MangoReward(Context mContext, @NonNull MGRewardListener listener) {
        this.mContext = mContext;
        mangoRewardListener = listener;
    }

    static MGRewardListener getMGMobRewardListener() {
        return mangoRewardListener;
    }

    public void load() {
        Api.getAdList(AdType.REWARD, false, new Api.CommonCallback<List<AdDataBean>>() {
            @Override
            public void onSuccess(List<AdDataBean> adDataBeans) {
                //判空
                if (adDataBeans.isEmpty()) {
                    mangoRewardListener.onError(mContext.getString(R.string.load_ad_list_is_empty));
                    return;
                }

                AdDataBean adDataBean = adDataBeans.get(0);
                //判空
                if (adDataBean == null) {
                    mangoRewardListener.onError(mContext.getString(R.string.load_ad_list_is_empty));
                    return;
                }
                //暂存下来
                mAdDataBean = adDataBean;
                mAdDataBean.setRequestTime(System.currentTimeMillis());

                if (isReady()) {
                    //通知广告请求好了
                    mangoRewardListener.onLoaded();
                } else {
                    mangoRewardListener.onError(mContext.getString(R.string.load_ad_list_is_empty));
                }
            }

            @Override
            public void onError(String errMsg) {
                MLog.logi("onError: " + errMsg);
                mangoRewardListener.onError(errMsg);
            }
        });
    }

    private boolean isReady() {
        if (mAdDataBean == null) {
            MLog.logi("广告不合法");
            return false;
        }

        if (mAdDataBean.getAdInfo() == null || mAdDataBean.getAdInfo().isEmpty()) {
            MLog.logi("广告不合法");
            return false;
        }

        AdDataBean.AdInfoBean adInfoBean = mAdDataBean.getAdInfo().get(0);

        if (adInfoBean == null || adInfoBean.getAdBaseInfo() == null || adInfoBean.getAdConversionInfo() == null || adInfoBean.getAdMaterialInfo() == null) {
            MLog.logi(mContext.getString(R.string.ad_invalid));
            return false;
        }

        int second = adInfoBean.getAdBaseInfo().getExpirationTime();
        long requestTime = mAdDataBean.getRequestTime();
        long currentTime = System.currentTimeMillis();
        if (second != 0 && currentTime - requestTime >= second * 1000) {
            MLog.logi("广告已经失效了");
            return false;
        }

        return true;
    }

    public boolean show() {
        if (!isReady()) {
            MLog.logi(mContext.getString(R.string.ad_invalid));
            return false;
        }

        MGRewardActivity.action(mContext, mAdDataBean);
        mAdDataBean = null;
        return true;
    }
}